<?php
/**
 * DiagnosticsController class.
 * DiagnosticsController is for diagnostics and crash report.
 * 
 * LBB Solutions Ltd. 版权所有 @ 2012.
 */
class DiagnosticsController extends ApiController
{
	public function actionCrash()
	{
		$device = Yii::app()->request->getParam('device');
		$token = Yii::app()->request->getParam('token');
		$email = Yii::app()->request->getParam('email');
		$message = Yii::app()->request->getParam('message');
		
		$android_version = Yii::app()->request->getPost('ANDROID_VERSION');
		if (!empty($android_version)) {
			$device = 'android';
			$message .= "\n" . print_r($_POST, 1);
		}
		
		$log_file = 'crash_log_'.$token.'_'.date('Ymd').'.txt';
		
		$body = "Yamii {$device} App crashed on " . date('Y-m-d') . ".\n";
		$body .= print_r(array(
			'client' => Yii::app()->request->getUserAgent(),
			'log_file' => 'http://yamii.fi/APILogs/crash/' . $log_file,
			'device' => $device,
			'token' => $token,
			'email' => $email
		), 1);
		
		$iPhoneDev = array('wenqing.alex.zhou@gmail.com', 'boc.chen@gmail.com');
		$AndroidDev = array('pkliang@gmail.com', 'boc.chen@gmail.com');
		$AllDev = array('pkliang@gmail.com', 'wenqing.alex.zhou@gmail.com', 'boc.chen@gmail.com');
		
		if ($device == 'ios') $email_list = $iPhoneDev;
		else if ($device == 'android') $email_list = $AndroidDev;
		else $email_list = $AllDev;
		
		// send out email
		BUtils::sendMaiToDevelopers("Yamii {$device} App Crash Report - " . date('Y-m-d'), $body, $email_list);
		
		// write down log
		$body .= "The detail report from client:\n";
		$body .= "==============================\n";
		$body .= $message;
		$body .= "\n==============================\n";
		self::APICrashing($body, $log_file);
		
		$this->_sendJSON(200, 'OK');
	}
}